#include "traccreatequerydialog.h"
#include "ui_traccreatequerydialog.h"

#include <QxtLogger>

// @@@ Question: do I want to grab the contents of all the elements when User finishes manipulating an
// individual widget, or when they say "OK"?

// following http://doc.trolltech.com/4.2/designer-using-a-component.html#the-single-inheritance-approach
TracCreateQueryDialog::TracCreateQueryDialog(QStringList types, QWidget *parent) :
    QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint),
    ui(new Ui::TracCreateQueryDialog)
{
    ui->setupUi(this);

    ui->typeComboBox->addItem("<any>");
    foreach (QString type, types)
    {
        ui->typeComboBox->addItem(type);
    }
}


TracCreateQueryDialog::~TracCreateQueryDialog()
{
    // qxtLog->write(QString("deleting the query dialog!"));
    delete ui;
}


void TracCreateQueryDialog::createQuery()
{
    queryPieces["summary"] = ui->summaryEntryBox->text();
    queryPieces["description"] = ui->descriptionEntryBox->text();
    queryPieces["type"] = ui->typeComboBox->currentText();
    queryPieces["owner"] = ui->ownerEntryBox->text();
    queryPieces["includeClosed"] = ui->closedTaskCheckbox->isChecked() ? QString("TRUE") : QString("FALSE");
    queryPieces["label"] = ui->queryLabel->text();

    QMapIterator<QString, QString> i(queryPieces);
    while(i.hasNext()) {
        i.next();
        qxtLog->write(i.key());
        qxtLog->write(i.value());
    }
    accept();

}

QMap<QString, QString> TracCreateQueryDialog::getQueryPieces()
{
    return queryPieces;
}

void TracCreateQueryDialog::on_queryLabel_editingFinished()
{
    if (ui->queryLabel->text().isNull() || ui->queryLabel->text().isEmpty())
    {
        ui->queryLabel->setText("Custom Query");
    }
}
